﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;
using System.Windows;

namespace ZhuXingFamilys.utils
{
    public class WaittingDlgHelper
    {
        /// <summary>
        /// 等待关闭时的回调
        /// </summary>
        public delegate void CloseWaitDialogCallBack();

        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hwnd, int index);
        [DllImport("user32.dll")]
        private static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

        private static WaittingDlg _WatitDlg;
        private static CloseWaitDialogCallBack _CallBack;

        /// <summary>
        /// 是否已激活等待对话框 
        /// - 调用本Helper内部函数时不需要对此判断
        /// - 仅对外部显示是否正在等待
        /// </summary>
        public static bool IsWaitAlive { get { return _WatitDlg != null; } }
        /// <summary>
        /// 显示等待对话框
        /// </summary>
        /// <param name="win"></param>
        /// <param name="sWaittingInfo"></param>
        /// <param name="sWindowColor"></param>
        /// <param name="sDlgBorderColor"></param>
        /// <param name="nBorderWidth"></param>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        public static void ShowWaitDialog(Window win, string sWaittingInfo = "", string sWindowColor = "", string sDlgBorderColor = "", int nBorderWidth = 5, int nWidth = 0, int nHeight = 0)
        {
            win.Dispatcher.Invoke(new Action(() =>
            {
                if (_WatitDlg != null)
                {
                    _WatitDlg.Close();
                    _WatitDlg = null;
                }
                IntPtr handle = (new System.Windows.Interop.WindowInteropHelper(win)).Handle;
                _WatitDlg = new WaittingDlg(sWaittingInfo, sWindowColor, sDlgBorderColor, nBorderWidth, nWidth, nHeight);
                _WatitDlg.Owner = win;
                _WatitDlg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                _WatitDlg.ShowInTaskbar = false;
                WindowInteropHelper helper = new WindowInteropHelper(_WatitDlg);
                EnableWindow(handle, false);
                _WatitDlg.ShowDialog();
            }));
        }
        /// <summary>
        /// 关闭等待对话框
        /// </summary>
        /// <param name="win"></param>
        public static void CloseWaitDialog(Window win)
        {
            win.Dispatcher.Invoke(new Action(() =>
            {
                if (_WatitDlg != null)
                {
                    _WatitDlg.Close();
                    _WatitDlg = null;
                }
                if (_CallBack != null)
                {
                    _CallBack();
                }
            }));
        }
        /// <summary>
        /// 设置对话框内容
        /// </summary>
        /// <param name="win"></param>
        /// <param name="strTitle"></param>
        public static void SetWaitDialogTitle(Window win, string strTitle = "")
        {
            win.Dispatcher.Invoke(new Action(() =>
            {
                if (_WatitDlg != null)
                {
                    if (strTitle != "")
                    {
                        _WatitDlg.sWaittingInfo = strTitle;
                    }
                }
            }));

        }
        /// <summary>
        /// 设置关闭等待对话框回调 - 收到关闭通知即回调
        /// </summary>
        public static void SetWaitDialogCloseCallBack(CloseWaitDialogCallBack CallBackMethod)
        {
            _CallBack += CallBackMethod;
        }
    }
}
